﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hardcodet.Wpf.TaskbarNotification;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

namespace FBApp.Classes
{
    public class Notifier
    {
        private TaskbarIcon tb = null;

        public Notifier()
        {
            try
            {                                
                tb = (TaskbarIcon)Application.Current.MainWindow.FindResource("tb");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading taskbaricon resource: " + ex.Message); 
            }
            
        }

        public void displayError(string error,string extra)
        {
            if (tb != null)
            {
                try
                {
                        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                            {
                                var balloon = new ErrorBalloon(error,extra);
                                tb.ShowCustomBalloon(balloon, PopupAnimation.Fade, 12000);
                            }
                        ));
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Error displaying error(lol): " + ex.Message);
                }
            }
        }

        public void displayNotification(string message)
        {            
            if (tb != null)
            {
                try
                {
                    if (message.Contains("link"))
                    {
                        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                        {
                            var balloon = new NotificationBalloon(NotificationBalloon.NotificationType.Link, message);
                            tb.ShowCustomBalloon(balloon, PopupAnimation.Fade, 12000);
                        }
                        ));
                    }
                    else if(message.Contains("like"))
                    {
                        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                            {
                                var balloon = new NotificationBalloon(NotificationBalloon.NotificationType.Like, message);
                                tb.ShowCustomBalloon(balloon, PopupAnimation.Fade, 12000);
                            }
                        ));                        
                    }
                    else if (message.Contains("comment"))
                    {
                        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                            {
                                var balloon = new NotificationBalloon(NotificationBalloon.NotificationType.Comment, message);
                                tb.ShowCustomBalloon(balloon, PopupAnimation.Fade, 12000);
                            }
                        ));
                    }
                    else if (message.Contains("wrote"))
                    {
                        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                        {
                            var balloon = new NotificationBalloon(NotificationBalloon.NotificationType.Comment, message);
                            tb.ShowCustomBalloon(balloon, PopupAnimation.Fade, 12000);
                        }
                          ));
                    }
                    else if (message.Contains("relationship"))
                    {
                        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                            {
                                var balloon = new NotificationBalloon(NotificationBalloon.NotificationType.Relationship, message);
                                tb.ShowCustomBalloon(balloon, PopupAnimation.Fade, 12000);
                            }
                        ));
                    }
                    else
                    {
                        Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                            {
                                var balloon = new NotificationBalloon(NotificationBalloon.NotificationType.Comment, message);
                                tb.ShowCustomBalloon(balloon, PopupAnimation.Fade, 12000);                             
                            }
                            ));
                    }                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error displaying notification: " + ex.Message);
                }
            }
        }

        public void displayNewsFeedNotification(string fromID, string fromName, string message)
        {
            if (tb != null)
            {
                try
                {
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                    {                        
                        BitmapImage bi = SharedFunctions.loadImage(fromID);
                        if (bi != null)
                        {
                            var balloon = new NewsFeedBalloon(fromID, fromName, message, bi);
                            tb.ShowCustomBalloon(balloon, PopupAnimation.Fade, 12000);                            
                        } 
                    }
                    ));

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error displaying News Feed notification: " + ex.Message);
                }
            }
        }

        public void displayMessageNotification(string fromID, string subject, string fromName, string message)
        {
            if (tb != null)
            {
                try
                {
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                    {
                        BitmapImage bi = SharedFunctions.loadImage(fromID);
                        if (bi != null)
                        {
                            var balloon = new MessageBalloon(fromID,subject, fromName, message, bi);
                            tb.ShowCustomBalloon(balloon, PopupAnimation.Fade, 12000);
                        }
                    }
                    ));

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error displaying Message Feed notification: " + ex.Message);
                }
            }
        }        

    }
}
